package com.example.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.domain.QuitClass;
import com.example.result.Result;
import com.example.service.QuitClassService;
import com.example.tool.GsonTool;

/**
 * @author 袁小弘
 * @describe 退班控制层
 * @date 2020-02-18
 */
@RestController
@RequestMapping("/api")
public class QuitClassController {

    @Autowired
    QuitClassService quitClassService;

    // ===============================退班管理==============================================

    /**
     * @param json
     * @return 添加成功则提示成功信息，反之则提示失败信息
     * @describe 添加退班信息
     */
    @RequestMapping(value = "/quits", method = RequestMethod.POST)
    public Result addQuitClass(@RequestBody String json) {
        // 将JSON数据转换为QuitClass对象实体
        JSONObject parseObject = JSON.parseObject(json);
        QuitClass quitClass = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), QuitClass.class);

        return quitClassService.addQuitClass(quitClass);

    }

    /**
     * @param json
     * @return 更新成功则提示成功信息，反之则提示失败信息
     * @describe 更新退班信息
     */
    @RequestMapping(value = "/quits", method = RequestMethod.PUT)
    public Result updateQuitClass(@RequestBody String json) {
        // 将JSON数据转换为QuitClass对象实体
        JSONObject parseObject = JSON.parseObject(json);
        QuitClass quitClass = GsonTool.getGsonInstance().fromJson(parseObject.getString("json"), QuitClass.class);
        return quitClassService.updateQuitClass(quitClass);

    }

    /**
     * @param quitId
     * @return 删除成功则提示成功信息，反之则提示失败信息
     * @describe 根据退班编号删除退班信息
     */
    @RequestMapping(value = "/quits/{quitId}", method = RequestMethod.DELETE)
    public Result deleteQuitClassById(@PathVariable("quitId") String quitId) {
        return quitClassService.deleteQuitClassById(quitId);

    }

    /**
     * @return 以JSON格式返回所有班级信息
     * @describe 查询所有退班信息
     */
    @RequestMapping(value = "/quits", method = RequestMethod.GET)
    public Result queryAllQuitClass() {
        return quitClassService.queryAllQuitClass();

    }

    /**
     * @param quitId
     * @return 以JSON格式返回该退班信息
     * @describe 根据编号查询退班信息
     */
    @RequestMapping(value = "/quits/{quitId}", method = RequestMethod.GET)
    public Result queryQuitClassById(@PathVariable("quitId") String quitId) {
        return quitClassService.queryQuitClassById(quitId);

    }

    /**
     * @param json
     * @return 添加成功则返回一个成功提示，反之则提示失败信息
     * @describe 通过Excel批量添加退班信息
     */
    @RequestMapping(value = "/quits/excels", method = RequestMethod.POST)
    public Result addQuitClassByExcel(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        List<QuitClass> list = JSON.parseArray(parseObject.getString("json"), QuitClass.class);

        System.out.println(list);

        return quitClassService.addQuitClassByExcel(list);

    }

    /**
     * @param Ids
     * @return 删除成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据退班编号批量删除退班信息
     */
    @RequestMapping(value = "/quits/dels", method = RequestMethod.POST)
    public Result deleteQuitClassByIds(@RequestBody Map<String, String> Ids) {

        return quitClassService.deleteQuitClassByIds(Ids);
    }

    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 退班信息模糊查询
     */
    @RequestMapping(value = "/quits/serachs", method = RequestMethod.POST)
    public Result serachByInfo(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        String[] info = parseObject.getString("json").split(",");

        return quitClassService.serachByInfo(info);

    }

    /**
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 查询所有退班和用户信息
     */
    @RequestMapping(value = "/quits/quitClassAndUsers", method = RequestMethod.GET)
    public Result queryAllQuitClassAndUsersInfo() {
        return quitClassService.queryAllQuitClassAndUsersInfo();
    }


    /**
     * @param json
     * @return 查询成功则返回一个成功提示，反之则提示失败信息
     * @describe 根据学生ID查询学生退班信息
     */
    @RequestMapping(value = "/quits/quitsClassByUserId", method = RequestMethod.POST)
    public Result selectQuitClassByUserId(@RequestBody String json) {
        System.out.println(json);
        JSONObject parseObject = JSON.parseObject(json);
        Integer userId = Integer.parseInt(parseObject.getString("json"));

        return quitClassService.selectQuitClassByUserId(userId);
    }
}
